package com.bjpowernode.controller;

import com.bjpowernode.beans.User;
import com.bjpowernode.dto.ResultDTO;
import com.bjpowernode.exception.LoginException;
import com.bjpowernode.services.UserServices;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/login")
public class LoginController {

    @Autowired
    @Qualifier("userServices")
    UserServices userServices;

    @RequestMapping("in.action")
    public ResultDTO in(HttpSession httpSession, HttpServletResponse response, String loginAct, String loginPwd, String rememberMe) throws LoginException {
        //System.out.println("rememberMe:"+rememberMe);
        User login = userServices.login(loginAct, loginPwd);
        if (login == null) {
            throw new LoginException("用户名或者密码错误");
        }
        //登录成功
        httpSession.setAttribute("user", login);
        //1——设置cookie的键值对（建和值必须只能是String类型）
        //2——设置cookie的键值对的作用域，也就是那个地址可以用(这里设置成根地址，意味着任何地址都可以调用cookie里面的值)
        //3——设置cookie的年龄（如果设置了年龄，且值为0，则实际上是删除cookie）
        Cookie cookieLoginAct = new Cookie("loginAct", loginAct);
        cookieLoginAct.setPath("/");
        Cookie cookieLoginPwd = new Cookie("loginPwd", DigestUtils.md5Hex(loginPwd));
        cookieLoginPwd.setPath("/");
        //判断是否需要记住用户（10天）
        if (rememberMe.equals("true")) {
            //记住用户
            cookieLoginAct.setMaxAge(60 * 60 * 24 * 10);
            cookieLoginPwd.setMaxAge(60 * 60 * 24 * 10);
        } else {
            //删除记住的用户
            cookieLoginAct.setMaxAge(0);
            cookieLoginPwd.setMaxAge(0);
        }
        //响应的时候告诉浏览器添加我们的cookie
        response.addCookie(cookieLoginAct);
        response.addCookie(cookieLoginPwd);


        ResultDTO resultDTO = new ResultDTO();
        resultDTO.setResult(ResultDTO.SUCCESS);
        resultDTO.setData(login);
        return resultDTO;
    }


    @RequestMapping("userInfo.action")
    public ResultDTO userInfo(HttpSession httpSession) throws LoginException {
        ResultDTO resultDTO = new ResultDTO();
        resultDTO.setResult(ResultDTO.SUCCESS);


        User login = (User) httpSession.getAttribute("user");

        if (login == null) {
            throw new LoginException("还没有登录");
        }

        resultDTO.setData(login);


        return resultDTO;
    }


    @RequestMapping("out.action")
    public ResultDTO out(HttpSession httpSession,HttpServletResponse response) {

        //删除session里面的数据
        httpSession.removeAttribute("user");
        //删除cookies
        Cookie cookieLoginAct = new Cookie("loginAct", "");
        cookieLoginAct.setPath("/");
        cookieLoginAct.setMaxAge(0);
        Cookie cookieLoginPwd = new Cookie("loginPwd", "");
        cookieLoginPwd.setPath("/");
        cookieLoginPwd.setMaxAge(0);
        response.addCookie(cookieLoginAct);
        response.addCookie(cookieLoginPwd);

        ResultDTO resultDTO = new ResultDTO();
        resultDTO.setResult(ResultDTO.SUCCESS);
        return resultDTO;
    }


}
